package com.utonw.carloan.utils;

import android.app.Activity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import com.utonw.carloan.R;
import com.utonw.carloan.app.MyApplication;
import com.utonw.carloan.function.GlobalLoanFunction;
import com.utonw.carloan.global.ReleaseData;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by SEELE on 2017/3/21.
 */

public abstract class ChooseBarUtils {
    //当前实例化的Activity对象
    private Activity mActivity;
    //Toggle选择按钮，返回 1,0
    private static final int TYPE_TOGGLE = 0;
    //是必填项
    private static final String IS_REQUIRED = "1";
    //不是必填项
    private static final String NOT_REQUIRED = "0";
    //普通EditText
    private static final int TYPE_EDIT_TEXT_WITH_ORI_TIPS = 1;
    //单选按钮，性别（M（男）/FM（女））（独立）
    private static final int TYPE_RADIO_BUTTON = 2;
    //带小相机图片的action
    private static final int TYPE_WITH_IMAGE = 3;
    //可替换EditText的hint内容
    private static final int TYPE_CHANGE_EDITTEXT_WORDS = 4;
    //手机验证码（独立）
    private static final int TYPE_VERIFICATION = 5;

    protected ChooseBarUtils(Activity activity) {
        mActivity = activity;
    }

    /**
     * 单功能ChooseBar，只有右侧 “请选择>” 字样
     *
     * @param resultToShowId 出现在展示页的顺序，填0不会出现在展示页，其他按升序排列显示
     * @param isRequired 是否是必填项
     * @param paramName 对应的字段名称
     * @param includeId include的布局id
     * @param sTitle    动作条左侧的标题
     *                  不指定页面将存入Content集合
     *                  此方法只会返回TextView上的值
     */
    public void initChooseBar(String resultToShowId,String isRequired,String paramName, final int includeId, int sTitle) {
        TextView barTitle = (TextView) mActivity.findViewById(includeId).findViewById(R.id.tv_choosebar_name);
        LinearLayout mLayout = (LinearLayout) mActivity.findViewById(includeId).findViewById(R.id.choosebar_action);
        barTitle.setText(MyApplication.getContext().getString(sTitle));
        final TextView tvToShow = (TextView) mLayout.findViewById(R.id.tv_choosebar_to_show);
        if (isRequired.equals(IS_REQUIRED)){
            getCheckedViewListener(includeId,sTitle,tvToShow);
        }
        if (!resultToShowId.equals("0")){
            HashMap<Integer,View> hashMap=new HashMap<>();
            hashMap.put(sTitle,tvToShow);
            ReleaseData.getAllResultContent.put(resultToShowId,hashMap);
        }
        mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChooseBarClickListener(includeId, tvToShow);
            }
        });
        tvToShow.setTag(paramName);
        ReleaseData.getAllTextViewContent.add(tvToShow);
    }

    /**
     * 单功能ChooseBar，只有右侧 “请选择>” 字样
     *
     * @param resultToShowId 出现在展示页的顺序，填0不会出现在展示页，其他按升序排列显示
     * @param isRequired 是否是必填项
     * @param paramName 对应的字段名称
     * @param pageId    指定page页面编号，将存入独立集合，填写-1将忽略此数据源
     * @param includeId include的布局id
     * @param sTitle    动作条左侧的标题
     *                  此方法只会返回TextView上的值
     */
    public void initChooseBar(String resultToShowId,String isRequired,String paramName, int pageId, final int includeId, int sTitle) {
        TextView barTitle = (TextView) mActivity.findViewById(includeId).findViewById(R.id.tv_choosebar_name);
        LinearLayout mLayout = (LinearLayout) mActivity.findViewById(includeId).findViewById(R.id.choosebar_action);
        barTitle.setText(MyApplication.getContext().getString(sTitle));
        final TextView tvToShow = (TextView) mLayout.findViewById(R.id.tv_choosebar_to_show);
        mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChooseBarClickListener(includeId, tvToShow);
            }
        });
        switch (pageId) {
            case 0:
                if (isRequired.equals(IS_REQUIRED)){
                    getCheckedViewListener(includeId,sTitle,tvToShow);
                }
                tvToShow.setTag(paramName);
                ReleaseData.getAllTextViewEntry.add(tvToShow);

                if (!resultToShowId.equals("0")){
                    HashMap<Integer,View> hashMap=new HashMap<>();
                    hashMap.put(sTitle,tvToShow);
                    ReleaseData.getAllResultEntry.put(resultToShowId,hashMap);
                }
                break;
        }


    }


    //单功能ChooseBar，右侧可选择功能模块：
    // 1.ToggleSwitch
    // 2.EditText
    // 3.RadioButton
    // 4.带图版“请选择>”
    // 5.可修改文字版“请选择>”

    /**
     * @param paramName   对应的字段名称
     * @param includeId   include的布局id
     * @param sTitle      动作条左侧的标题
     * @param resultState 确定右侧布局表现形式
     * @param editWords   可修改部分如Hint文字
     */
    public void initChooseBar(String resultToShowId,String isRequired,String paramName, final int includeId, final int sTitle, ResultState resultState, String editWords) {
        TextView barTitle = (TextView) mActivity.findViewById(includeId).findViewById(R.id.tv_choosebar_name);
        barTitle.setText(MyApplication.getContext().getString(sTitle));
        FrameLayout mLayout = (FrameLayout) mActivity.findViewById(includeId).findViewById(R.id.fl_choosebar_replace);
        //根据参数替换对应的action功能
        switch (resultState.getModeType()) {
            //滑动开关
            case TYPE_TOGGLE:
                changeLayout(includeId, R.layout.choosebar_item_right_toggle);
                Switch tempSw = (Switch) mActivity.findViewById(includeId).findViewById(R.id.sw_choosebar_right_toggle);
                tempSw.setTag(paramName);
                tempSw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        onToggleButtonListener(includeId, isChecked);
                    }
                });
                ReleaseData.getAllSwitchContent.add(tempSw);
                break;

            //带提示的EditText
            case TYPE_EDIT_TEXT_WITH_ORI_TIPS:
                changeLayout(includeId, R.layout.choosebar_item_right_edittext_with_tips, editWords);
                EditText tempEt1 = (EditText) mActivity.findViewById(includeId).findViewById(R.id.et_item_please_input);
                tempEt1.setTag(paramName);
                ReleaseData.getAllEditTextContent.add(tempEt1);
                setInputMode(includeId, tempEt1);
                if (isRequired.equals(IS_REQUIRED)){
                    getCheckedViewListener(includeId, sTitle,tempEt1);
                }
                if (!resultToShowId.equals("0")){
                    HashMap<Integer,View> hashMap=new HashMap<>();
                    hashMap.put(sTitle,tempEt1);
                    ReleaseData.getAllResultContent.put(resultToShowId,hashMap);
                }
                break;

            //单项选择器（性别）
            case TYPE_RADIO_BUTTON:
                changeLayout(includeId, R.layout.choosebar_item_right_radiobutton);
                RadioGroup tempGroup = (RadioGroup) mActivity.findViewById(includeId).findViewById(R.id.rg_right_radiogroup);
                RadioButton rMale= (RadioButton) tempGroup.findViewById(R.id.rbtn_male);
                RadioButton rFemale= (RadioButton) tempGroup.findViewById(R.id.rbtn_female);
                tempGroup.setTag(paramName);
                onRadioButtonListener(rMale,rFemale);
                ReleaseData.getAllRadioGroupContent.add(tempGroup);
                break;

            //带小相机图片
            case TYPE_WITH_IMAGE:
                Map<Object,Object> map=new HashMap<>();
                map.put("paramName",paramName);
                map.put("isUpload",false);
                map.put("base64",null);
                map.put("filePath",null);
                changeLayout(includeId, R.layout.choosebar_item_right_with_image);
                final ImageView mIndicator = (ImageView) mActivity.findViewById(includeId).findViewById(R.id.iv_image_upload_indicator);
                final TextView mInstruction = (TextView) mActivity.findViewById(includeId).findViewById(R.id.tv_image_upload_instruction);
                mInstruction.setTag(map);
                ReleaseData.getAllImageUpload.add(mInstruction);
                mLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onImageChooseBarListener(includeId,mIndicator,mInstruction,sTitle);
                    }
                });
                break;

            //可替换EditText的hint字段
            case TYPE_CHANGE_EDITTEXT_WORDS:
                changeLayout(includeId, R.layout.choosebar_item_right_edittext, editWords);
                EditText tempEt2 = (EditText) mActivity.findViewById(includeId).findViewById(R.id.et_item_please_input_none);
                tempEt2.setTag(paramName);
                ReleaseData.getAllEditTextContent.add(tempEt2);
                setInputMode(includeId, tempEt2);
                if (isRequired.equals(IS_REQUIRED)){
                    getCheckedViewListener(includeId, sTitle,tempEt2);
                }
                if (!resultToShowId.equals("0")){
                    HashMap<Integer,View> hashMap=new HashMap<>();
                    hashMap.put(sTitle,tempEt2);
                    ReleaseData.getAllResultContent.put(resultToShowId,hashMap);
                }
                break;

            //验证码专属
            case TYPE_VERIFICATION:
                changeLayout(includeId, R.layout.choosebar_item_right_edittext_with_verification);
                break;


        }


    }

    /**
     * @param paramName   对应的字段名称
     * @param pageId      指定page页面编号，将存入独立集合，
     *                    填写-1将忽略此数据源
     *                    填写0将会存储到Entry页的独立集合中
     *                    填写1将存储到垃圾集合中，不会存储到提交集合
     *                    不填将存储到Content集合中
     *
     * @param includeId   include的布局id
     * @param sTitle      动作条左侧的标题
     * @param resultState 确定右侧布局表现形式
     * @param editWords   可修改部分如Hint文字
     */
    public void initChooseBar(String resultToShowId,String isRequired,String paramName, int pageId, final int includeId, final int sTitle, ResultState resultState, String editWords) {
        TextView barTitle = (TextView) mActivity.findViewById(includeId).findViewById(R.id.tv_choosebar_name);
        barTitle.setText(MyApplication.getContext().getString(sTitle));
        FrameLayout mLayout = (FrameLayout) mActivity.findViewById(includeId).findViewById(R.id.fl_choosebar_replace);
        //根据参数替换对应的action功能
        switch (resultState.getModeType()) {
            //滑动开关
            case TYPE_TOGGLE:
                changeLayout(includeId, R.layout.choosebar_item_right_toggle);
                Switch tempSw = (Switch) mActivity.findViewById(includeId).findViewById(R.id.sw_choosebar_right_toggle);
                tempSw.setTag(paramName);
                tempSw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        onToggleButtonListener(includeId, isChecked);
                    }
                });
                switch (pageId) {
                    case 0:
                        ReleaseData.getAllSwitchEntry.add(tempSw);
                        break;

                    case 1:
                        ReleaseData.tempUnused.add(tempSw);
                        break;
                }
                break;

            // 带提示的EditText
            case TYPE_EDIT_TEXT_WITH_ORI_TIPS:
                changeLayout(includeId, R.layout.choosebar_item_right_edittext_with_tips, editWords);
                switch (pageId) {
                    case 0:
                        EditText tempEt1 = (EditText) mActivity.findViewById(includeId).findViewById(R.id.et_item_please_input);
                        tempEt1.setTag(paramName);
                        ReleaseData.getAllEditTextEntry.add(tempEt1);
                        setInputMode(includeId, tempEt1);
                        if (isRequired.equals(IS_REQUIRED)){
                            getCheckedViewListener(includeId, sTitle, tempEt1);
                        }

                        if (!resultToShowId.equals("0")){
                            HashMap<Integer,View> hashMap=new HashMap<>();
                            hashMap.put(sTitle,tempEt1);
                            ReleaseData.getAllResultEntry.put(resultToShowId,hashMap);
                        }
                        break;

                }

                break;

            //单项选择器（性别）
            case TYPE_RADIO_BUTTON:
                changeLayout(includeId, R.layout.choosebar_item_right_radiobutton);
                switch (pageId) {
                    case 0:
                        RadioGroup tempGroup = (RadioGroup) mActivity.findViewById(includeId).findViewById(R.id.rg_right_radiogroup);
                        tempGroup.setTag(paramName);
                        ReleaseData.getAllRadioGroupEntry.add(tempGroup);
                        break;

                }
                break;

            //带小相机图片
            case TYPE_WITH_IMAGE:
                Map<Object,Object> map=new HashMap<>();
                map.put("paramName",paramName);
                map.put("isUpload",false);
                map.put("base64",null);
                map.put("filePath",null);
                changeLayout(includeId, R.layout.choosebar_item_right_with_image);
                final ImageView mIndicator = (ImageView) mActivity.findViewById(includeId).findViewById(R.id.iv_image_upload_indicator);
                final TextView mInstruction = (TextView) mActivity.findViewById(includeId).findViewById(R.id.tv_image_upload_instruction);
                mInstruction.setTag(map);
                ReleaseData.getAllImageUpload.add(mInstruction);
                mLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onImageChooseBarListener(includeId, mIndicator, mInstruction, sTitle);
                    }
                });
                break;

            //可替换EditText的hint字段
            case TYPE_CHANGE_EDITTEXT_WORDS:
                changeLayout(includeId, R.layout.choosebar_item_right_edittext, editWords);
                switch (pageId) {
                    case 0:
                        EditText tempEt1 = (EditText) mActivity.findViewById(includeId).findViewById(R.id.et_item_please_input_none);
                        tempEt1.setTag(paramName);
                        ReleaseData.getAllEditTextEntry.add(tempEt1);
                        setInputMode(includeId, tempEt1);
                        if (isRequired.equals(IS_REQUIRED)){
                            getCheckedViewListener(includeId, sTitle, tempEt1);
                        }

                        if (!resultToShowId.equals("0")){
                            HashMap<Integer,View> hashMap=new HashMap<>();
                            hashMap.put(sTitle,tempEt1);
                            ReleaseData.getAllResultEntry.put(resultToShowId,hashMap);
                        }
                        break;

                }
                break;

            //验证码专属
            case TYPE_VERIFICATION:
                changeLayout(includeId, R.layout.choosebar_item_right_edittext_with_verification);
                break;


        }


    }


    public void initComplexChooseBar(final int includeId, int sTitle, String param1, String param2) {
        TextView loadPrice = (TextView) mActivity.findViewById(includeId).findViewById(R.id.tv_function_name);
        TextView tParam1 = (TextView) mActivity.findViewById(includeId).findViewById(R.id.tv_function_people_sum);
        TextView tParam2 = (TextView) mActivity.findViewById(includeId).findViewById(R.id.tv_function_rate);
        LinearLayout mLayout = (LinearLayout) mActivity.findViewById(includeId).findViewById(R.id.ll_choosebar_complex_action);

        loadPrice.setText(MyApplication.getContext().getString(sTitle));
        tParam1.setText(param1);
        tParam2.setText(param2);
        mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChooseBarClickListener(includeId);
            }
        });
    }

    //设置普通监听事件
    protected abstract void setChooseBarClickListener(int view);

    //设置监听事件,回调要显示的TextView
    protected abstract void setChooseBarClickListener(int view, TextView toShow);

    //回调需要检测的View
    protected abstract void getCheckedViewListener(int includeId, int sTitle, View toCheck);

    // 1.ToggleSwitch
    public abstract void onToggleButtonListener(int buttonView, boolean isChecked);

    // 2.EditText
    public abstract void getEditTextWithTipsListener(int includeId, EditText tempEt1);

    // 3.RadioButton
    public abstract void onRadioButtonListener(RadioButton rMale, RadioButton rFemale);

    // 4.带图版“请选择>”
    public abstract void onImageChooseBarListener(int view, ImageView mIndicator, TextView mInstruction, int sTitle);

    // 5.可修改文字版“请选择>”
    public abstract void getChangeWordsEditTextListener(int includeId, EditText tempEt1, int sTitle);

    // 6.获取验证码独立界面
    public abstract void setVerificationListener();


    public enum ResultState {
        TOGGLE_BUTTON(0),
        EDIT_TEXT_WITH_TIPS(1),
        RADIO_BUTTON(2),
        ORI_WITH_IMAGE(3),
        CHANGE_WORDS(4),
        VERIFICATION(5);

        private int modeType;

        ResultState(int modeType) {
            this.modeType = modeType;
        }

        int getModeType() {
            return modeType;
        }


    }


    private void changeLayout(int includeId, int replaceLayout) {
        ViewUtils.initContentView(mActivity, includeId, R.id.fl_choosebar_replace, replaceLayout);

    }

    private void changeLayout(int includeId, int replaceLayout, String editWords) {
        ViewUtils.initContentView(mActivity, includeId, R.id.fl_choosebar_replace, replaceLayout, editWords);
    }

    /**
     * 设置EditText的输入模式
     *
     * @param includeId 传入的引用布局id
     * @param editText  要设置的EditText
     */
    private void setInputMode(int includeId, EditText editText) {
        switch (includeId) {
            //借款金额
            case R.id.jiekuanjine:
                GlobalLoanFunction.setPricePoint(editText);
                break;

            //月利率
            case R.id.yuelilv:
                GlobalLoanFunction.setPricePoint(editText);
                break;

            //GPS流量费
            case R.id.gpsliuliangfei:
                GlobalLoanFunction.setPricePoint(editText);
                break;

            //综合服务费
            case R.id.zonghefuwufei:
                GlobalLoanFunction.setPricePoint(editText);
                break;

            //办理费
            case R.id.banlifei:
                GlobalLoanFunction.setPricePoint(editText);
                break;

            //办理费
            case R.id.tingchefei:
                GlobalLoanFunction.setPricePoint(editText);
                break;

            //住宅电话
            case R.id.zhuzhaidianhua:
                GlobalLoanFunction.setNumberPoint(editText);
                break;

            //本地居住时间
            case R.id.bendijuzhushijian:
                GlobalLoanFunction.setNumberPoint(editText);
                break;

            //子女个数
            case R.id.zinvgeshu:
                GlobalLoanFunction.setNumberPoint(editText);
                break;

            //月租
            case R.id.yuezu:
                GlobalLoanFunction.setNumberPoint(editText);
                break;

            //现地址居住时间
            case R.id.xiandizhijuzhushijian:
                GlobalLoanFunction.setNumberPoint(editText);
                break;

            //房产数量
            case R.id.fangchanshuliang:
                GlobalLoanFunction.setNumberPoint(editText);
                break;

            //月供
            case R.id.yuegong:
                GlobalLoanFunction.setPricePoint(editText);
                break;

            //购买价格
            case R.id.goumaijiage:
                GlobalLoanFunction.setPricePoint(editText);
                break;

            //年收入
            case R.id.nianshouru:
                GlobalLoanFunction.setPricePoint(editText);
                break;

            //手机号码
            case R.id.shoujihaoma:
                GlobalLoanFunction.setNumberPoint(editText);
                break;


        }


    }


}
